package Dao;

import Model.StudentCourse;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class OnePara extends BaseDao<StudentCourse> {
    private StudentCourse changeName = new StudentCourse();


    /**
     * 添加学生到student表
     * @author 赵文赫
     * @param student
     * sql ? :String类型
     */
    public void insertStudent(String student) {
        String sql = "INSERT INTO student_course(student) VALUES (?)";
        changeName.setName(student);
        alter(sql, changeName);
    }

    /**
     * @author 赵文赫
     * 删除学生
     * sql ? :String类型
     * @param student
     */
    public void deleteStudent(String student) {
        String sql = "DELETE FROM student_course WHERE student = ?";
        changeName.setName(student);
        alter(sql, changeName);
    }



    /**
     * @author 赵文赫
     *显示一个学生的成绩
     * @param stuName
     * @return
     */
    public ArrayList<StudentCourse> selectOne(String stuName){
        String sql = "SELECT * FROM student_course WHERE student = ? ";
        changeName.setName(stuName);
        ArrayList<StudentCourse> courseArrayList = select(sql, changeName);
        return courseArrayList;
    }

    /**
     * 方法归到一个类里是因为preparedStatement只传一个parameter
     * @author 赵文赫
     * @param preparedStatement 因为preparedStatement需要传进去的参数不同所以在子类里重写
     * @param Object
     */
    @Override
    public void insert(PreparedStatement preparedStatement, StudentCourse Object) {
        try {
            preparedStatement.setString(1, Object.getName());

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 因为列数不固定，所以先获取列的数量count，然后循环存入StudentCourse对象里的ArrayList
     * @param resultSet
     * @param count
     * @return
     */
    @Override
    public StudentCourse setObjectByResultSet(ResultSet resultSet, int count) {
        StudentCourse studentCourse = new StudentCourse();
        try {
            studentCourse.setName(resultSet.getString(2));
            while(count>4)
            {//循环存入

                studentCourse.setCourseList(resultSet.getInt(count));
                count--;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return studentCourse;
    }
}
